package com.infomanage.service.tech;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.PersonDAO;
import com.infomanage.dao.TechExchangeDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.Person;
import com.infomanage.po.TechExchange;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.TechExchangeItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class TechExchangeService {

	public static TechExchangeItem loadTechExchange(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			TechExchangeDAO dao = new TechExchangeDAO();
			if (StringUtils.isEmpty(itemId))
				return null;
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<TechExchangeItem> loadTechExchangeList(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			TechExchangeDAO dao = new TechExchangeDAO();
			List<TechExchangeItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject saveTechExchange(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			TechExchange bean = new TechExchange();
			bean.setAcceptOndutyPerson(Integer.parseInt(request.getParameter("acceptOndutyPerson")));
			bean.setAcceptSuggest(request.getParameter("acceptSuggest"));
			bean.setAcceptXdPersonNum(Integer.parseInt(request.getParameter("acceptXdPersonNum")));
			bean.setAcceptXdzId(Integer.parseInt(request.getParameter("acceptXdzId")));
			bean.setAcceptZdzId(Integer.parseInt(request.getParameter("acceptZdzId")));
			bean.setConfirm(request.getParameter("confirm"));
			bean.setConfirmSign(request.getParameter("confirmSign"));
			bean.setDeliOndutyPerson(Integer.parseInt(request.getParameter("deliOndutyPerson")));
			bean.setDeliSuggest(request.getParameter("deliSuggest"));
			bean.setDeliXdPersonNum(Integer.parseInt(request.getParameter("deliXdPersonNum")));
			bean.setDeliXdzId(Integer.parseInt(request.getParameter("deliXdzId")));
			bean.setDeliZdzId(Integer.parseInt(request.getParameter("deliZdzId")));
			bean.setProblem(request.getParameter("problem"));
			bean.setProblemSign(request.getParameter("problemSign"));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("acceptXdzId")));
			if(person != null){
				bean.setDeptId(person.getDeptId());
			}
			TechExchangeDAO dao = new TechExchangeDAO();
			dao.add(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject removeTechExchange(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String studyId = request.getParameter("itemId");
			TechExchangeDAO dao = new TechExchangeDAO();
			dao.remove(TechExchange.class, Integer.parseInt(studyId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject removeBatchTechExchange(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			TechExchangeDAO dao = new TechExchangeDAO();
			if (batchIds != null) {
				String[] ids = batchIds.split(";");
				for (String id : ids) {
					dao.remove(TechExchange.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject modifyTechExchange(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("itemId"));
			TechExchangeDAO dao = new TechExchangeDAO();
			TechExchange bean = dao.find(TechExchange.class, planId);
			bean.setAcceptOndutyPerson(Integer.parseInt(request.getParameter("acceptOndutyPerson")));
			bean.setAcceptSuggest(request.getParameter("acceptSuggest"));
			bean.setAcceptXdPersonNum(Integer.parseInt(request.getParameter("acceptXdPersonNum")));
			bean.setAcceptXdzId(Integer.parseInt(request.getParameter("acceptXdzId")));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("acceptXdzId")));
			if(person != null){
				bean.setDeptId(person.getDeptId());
			}
			bean.setAcceptZdzId(Integer.parseInt(request.getParameter("acceptZdzId")));
			bean.setConfirm(request.getParameter("confirm"));
			bean.setConfirmSign(request.getParameter("confirmSign"));
			bean.setDeliOndutyPerson(Integer.parseInt(request.getParameter("deliOndutyPerson")));
			bean.setDeliSuggest(request.getParameter("deliSuggest"));
			bean.setDeliXdPersonNum(Integer.parseInt(request.getParameter("deliXdPersonNum")));
			bean.setDeliXdzId(Integer.parseInt(request.getParameter("deliXdzId")));
			bean.setDeliZdzId(Integer.parseInt(request.getParameter("deliZdzId")));
			bean.setProblem(request.getParameter("problem"));
			bean.setProblemSign(request.getParameter("problemSign"));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static List<TechExchangeItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			TechExchangeDAO dao = new TechExchangeDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<TechExchangeItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			TechExchangeDAO dao = new TechExchangeDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1) / PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String deliXdzId = request.getParameter("deliXdzId");
		String acceptXdzId = request.getParameter("acceptXdzId");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(deliXdzId)) {
			FieldBean field = new FieldBean("deliXdzId", deliXdzId);
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(acceptXdzId)) {
			FieldBean field = new FieldBean("acceptXdzId", acceptXdzId);
			searchBean.add(field);
		}

		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
